Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: improve miotdevice API, add gosund plug support (cuco.plug.cp1) #672

Closed
wants to merge 5 commits into from

Conversation

rytilahti
Copy link
Owner

@rytilahti rytilahti commented Apr 18, 2020

This PR aims to add better support for miot devices, and build a common API to simplify contributions for new devices.

  • add miot_info() to get the common information from the device

    • this includes manufacturer, model, firmeware version and serial number
  • add get_properties_for_dataclass(cls) which allows easy implementation for get_properties mappings

    • each field() can define metadata containing siid and piid, these are mapped automatically to the response container
    • _siid can be used to define common siid, _max_properties can be used to set number of maximum properties per request
  • "old" get_properties_for_mapping() requires explicit passing of the mapping, no more passing over __init__

  • rename old set_property to set_property_from_mapping

    • add set_properties_from_dataclass (allows passing a mapping object with all wanted values at once)
    device.set_properties_from_dataclass(GosundPlugStatus(state=True, some_other_property=123)
    
    • new set_property helper which takes kwargs that are used automatically with the help of the class-given _MAPPING:
    device.set_property(state=True)
    
  • Add support for gosund miot plug (cuco.plug.cp1) to show how the API could work.

TODO

  • Naming of helper methods, _MAPPING
  • Unit tests for all MiotDevice interfaces
  • Split set property requests based on _max_properties.
  • python 3.6 compatibility (dataclasses are available only python 3.7+)
  • helper to create fields with wanted metadata, maybe add_property(piid, siid)?
  • document the API, figure out the best way to do conversions, if needed.
  • handling read-only/write-only properties
  • common API for actions

* add miot_info() to get the common information from the device
  * this includes manufacturer, model, firmeware version and serial number

* add get_properties_for_dataclass(cls) which allows easy implementation for get_properties mappings
  * each field() can define metadata containing siid and piid, these are mapped automatically to the response container
  * _siid can be used to define common siid, _max_properties can be used to set number of maximum properties per request

* get_properties_for_mapping() requires explicit passing of the mapping, no more passing over __init__
@coveralls
Copy link

coveralls commented Apr 18, 2020

Coverage Status

Coverage increased (+0.005%) to 73.612% when pulling c7ba649 on miot_basics into 8f16c1b on master.

@rytilahti rytilahti changed the title improve miotdevice with some helpers WIP: improve miotdevice with some helpers Apr 18, 2020
* rename old set_property to set_property_from_mapping
* add set_properties_from_dataclass (allows passing a mapping object with all wanted values at once)

```
device.set_properties_from_dataclass(GosundPlugStatus(state=True, some_other_property=123)
```

* new set_property helper which takes kwargs that are used automatically with the help of the class-given _MAPPING:

```
device.set_property(state=True)
```
this is just a poc to show how the new api could function
@rytilahti rytilahti changed the title WIP: improve miotdevice with some helpers WIP: improve miotdevice API, add gosund plug support (cuco.plug.cp1) Apr 18, 2020
@rytilahti
Copy link
Owner Author

@foxel if you have some time, your feedback on how/if the API needs adaptations to allow porting airpurifier_miot over it :-) for the time being this is just an experimental try to see if this approach makes sense in general, alas, the only miot device I have has only a single service (besides the common miot_info) with a single property...

@marcelrv
Copy link

FYI.
For the zhimi.airpurifier.mb3 all siids respond with value exept for siid 1. siid1 does not respond to get_properties so below ones are not available in reality but are in the spec file.

Device Information-Current Firmware Version
Device Information-Device Manufacturer
Device Information-Device Model
Device Information-Device Serial Number

@rytilahti
Copy link
Owner Author

rytilahti commented Apr 18, 2020

Yeah, probably not all manufacturers implement it properly (considering miio.info also reveals enough information). Here's what I'm getting out with that plug:

MiotInfo(manufacturer='hello', model='hello', serial_number='hello', firmware_version='hello')

edit: also calling the toggle action on this device will do toggle the state, but there is no response besides the timeout error: {'code': -9999, 'message': 'user ack timeout'}

@xuefer
Copy link

xuefer commented Aug 30, 2020

the code looks much easier for miot in this branch, can you improve it asap? i want to add more devices like curtain wifi version
i wonder if someone already modify devtools/miottemplate.py to generate for new miio.miot_device class instead of for non-existing miio.MiOTService class

@rytilahti
Copy link
Owner Author

I'll try to find some time soonish to push this onwards. I have a locally modified devtools that will make integrating a bit easier. I'll create another PR for that when I can.

@mkohns
Copy link

mkohns commented Nov 5, 2022

Hi. Just an idea: What about downloading and parsing the miot spec on new device detection? On this page:
https://home.miot-spec.com/s/<model>
the spec JSON can be retrieved. With a small parser the full device feature set could be utilized.

@rytilahti
Copy link
Owner Author

@mkohns see the soon-to-be-linked PR, I'm closing this now as obsolete :-)

@rytilahti rytilahti closed this Nov 8, 2022
@rytilahti rytilahti deleted the miot_basics branch November 8, 2022 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants